package be.ad.coffeebreak.web.commands;

import be.ad.coffeebreak.domain.Project;
import be.ad.coffeebreak.domain.User;
import be.ad.coffeebreak.services.ProjectsService;
import be.ad.coffeebreak.web.Command;
import be.ad.coffeebreak.web.ServiceLocator;
import be.ad.coffeebreak.web.WebConstants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Stefan Bangels
 */
public final class ProjectsListCommand implements Command {

    protected ServiceLocator serviceLocator;

    public ProjectsListCommand(ServiceLocator serviceLocator) {
        this.serviceLocator = serviceLocator;
    }

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        User user = (User) request.getAttribute(WebConstants.ATTRIBUTE_USER);
        ProjectsService projectsService = serviceLocator.getProjectsService();
        List<Project> projects = projectsService.findProjectsByUserId(user.getId());
        request.setAttribute(WebConstants.ATTRIBUTE_PROJECTS, projects);
        return WebConstants.VIEW_PROJECTS;
    }

}
